let reg={
  id: /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/,
  phone:/^1(3|4|5|6|7|8|9)\d{9}$/
}
// 格式化时间日期
const formatTime = (date,formatype) => {
  let fomatDate = new Date(date);
  const year = fomatDate.getFullYear()
  const month = fomatDate.getMonth() + 1 < 10 ? '0' + (fomatDate.getMonth() + 1) : fomatDate.getMonth() + 1
  const day = fomatDate.getDate() < 10 ? '0' + fomatDate.getDate() : fomatDate.getDate()
  const hour = fomatDate.getHours() < 10 ? '0' + fomatDate.getHours() : fomatDate.getHours()
  const minute = fomatDate.getMinutes() < 10 ? '0' + fomatDate.getMinutes() : fomatDate.getMinutes()
  const second = fomatDate.getSeconds() < 10 ? '0' + fomatDate.getSeconds() : fomatDate.getSeconds()
  switch (formatype){
    case "YYYY-MM-DD hh:mm:ss":
      return `${year}-${month}-${day} ${hour}:${minute}:${second}`;
    break;
    case "YYYY年MM月DD日":
      return `${year}年${month}月${day}日`;
      break;
    case "YYYY.MM.DD":
      return `${year}.${month}.${day}`;
      break;
  }
  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}
// 格式化内容显示省略号
const formateContent = (content, number) => {
  
  let result = content.slice(0, number - 1) + (content.length < number?'':'...');
  return result;
}
/**微信支付封装接口 */
function wxPay(payParams){
  return new Promise((resole, reject)=>{
    wx.requestPayment({
      timeStamp: payParams.timeStamp,
      nonceStr: payParams.nonceStr,
      package: payParams.package,
      signType: payParams.signType,
      paySign: payParams.paySign,
      success(res) {
        wx.showToast({
          title: '支付成功',
          icon: 'success',
          duration: 2000,
          success(){
            resole(res);
          }
        })
       
      },
      fail(error) {
        wx.showToast({
          title: '支付失败',
          icon: 'none',
          duration: 2000,
          success() {
            reject(error);
          }
        }) 
      }
    })
  })
}
/**获取订单状态值对象**/
function getOrderStatus(status){
  let statusList=[{
    code:'50700101',
    label:'未支付',
    className:'cl-red'
  },{
      code: '50700102',
      label: '已支付',
      className: 'cl-theme'
  },{
      code: '50700103',
      label: '已过期',
      className: 'dec'
  }];
  return statusList.filter((item, index, array)=>{
    return (item.code === status)
  })
}
/**我的代金券使用状态值对象*/
function getVouchersStatus(status){
  let statusList = [{
    code: '50300101',
    label: '未使用',
    className: 'cl-red'
  }, {
      code: '50300102',
      label: '已使用',
    className: 'cl-theme'
  }, {
      code: '50300103',
      label: '已过期',
    className: 'dec'
  }];
  return statusList.filter((item, index, array) => {
    return (item.code === status)
  })
} 
/*获取用户信息封装**/ 
function getUserInfo(){
  return new Promise((resole, reject) => {
    wx.getSetting({
      success(res) {
        if (res.authSetting['scope.userInfo']) {
          wx.getUserInfo({
            success(result) {
              resole(result);
            },
            fail(error){
              reject(error);
            }
          })
        }else{
          wx.navigateTo({
            url: `/pages/authorize/authorize`,
          })
        }
      }
    })
  })
}
module.exports = {
  reg,
  wxPay,
  getOrderStatus,
  getUserInfo,
  formatTime,
  formateContent,
  getVouchersStatus
}
